package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.dao.CustomerDao;
import io.renren.modules.sys.entity.AppointmentEntity;
import io.renren.modules.sys.entity.CustomerEntity;
import io.renren.modules.sys.entity.CustomerEntity2;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.AppointmentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-26 23:42:54
 */
@RestController
@RequestMapping("sys/appointment")
public class AppointmentController  extends AbstractController  {
    @Autowired
    private AppointmentService appointmentService;
    
    @Autowired
    private CustomerDao customerDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
		Map<String, Object> params2=new HashMap<>();
		params2.put("date", new Date());
		PageUtils pageUtils=appointmentService.queryPage(params2);
		for (Object appointmentEntity : pageUtils.getList()) {
			AppointmentEntity appointmentEntity2=(AppointmentEntity) appointmentEntity;
			appointmentService.deleteById(appointmentEntity2.getId());
		}
    	
    	SysUserEntity sysUserEntity=getUser();
    	params.put("userId", sysUserEntity.getUserId());
        PageUtils page = appointmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        AppointmentEntity appointment = appointmentService.selectById(id);

        return R.ok().put("appointment", appointment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AppointmentEntity appointment){
    	SysUserEntity sysUserEntity=getUser();
    	appointment.setAddDate(new Date());
    	appointment.setUserId(sysUserEntity.getUserId().intValue());
    	appointment.setUserName(sysUserEntity.getUsername());
    	CustomerEntity customerEntity=customerDao.selectById(appointment.getCustomerId());
    	appointment.setCustomerName(customerEntity.getName());
        appointmentService.insert(appointment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AppointmentEntity appointment){
        ValidatorUtils.validateEntity(appointment);
        appointmentService.updateAllColumnById(appointment);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        appointmentService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
